import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'
import { WinsleiLogManager } from '@winslei/winsleilog'
import { WinsleiUtilsManager } from '@winslei/winsleiutils'
import window from '@ohos.window';
import { KeyConstants } from '../common/constants/KeyConstants'

const TAG = 'ServiceExtension'

export default class ServiceExtension extends ServiceExtensionAbility {
    // lifecycle
    async onCreate(want) {
        WinsleiLogManager.hilogIWithTag(TAG, 'onCreate')

        globalThis.context = this.context

        await this.initWindow()
    }

    onRequest(want, startId) {

    }

    onConnect(want) {
        return null;
    }
    onDisconnect(want) {

    }

    onDestroy() {

    }

    // private methods
    private async initWindow() {
        let win = await window.createWindow({
            name: KeyConstants.MAIN_WINDOW_NAME,
            windowType: window.WindowType.TYPE_FLOAT,
            ctx: this.context
        })
        await win.resize(WinsleiUtilsManager.display().width(), WinsleiUtilsManager.display().height() * 0.5)
        await win.moveWindowTo(0, WinsleiUtilsManager.display().height() * 0.5)
        await win.loadContent('pages/index', new LocalStorage())
        await win.showWindow()
        await win.setWindowTouchable(false)
    }
}